package com.breadenglish.cms.api.app.sys.manager;

//import com.baidu.ueditor.ActionEnter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Map;

/**
 * 作者: guo yao
 * 日期: 2015年12月29日
 * 时间: 下午11:29
 */
@Controller
@RequestMapping("/demo")
public class DemoController {

    public static BASE64Encoder encoder = new sun.misc.BASE64Encoder();
    public static BASE64Decoder decoder = new sun.misc.BASE64Decoder();


    @RequestMapping(value = "/demoCroppic.html")
    public String doDemoList() {
        return "demo/croppic";
    }

    @RequestMapping(value = "/photoClip.html")
    public String photoClip() {
        return "demo/photo_clip";
    }

    @RequestMapping(value = "/savePis.html")
    public Map<String, String> doUploadHandle(HttpServletRequest request, HttpServletResponse response) throws Throwable {
        String imgFile = request.getParameter("fileDate");
        // 将字符串转换成二进制，用于显示图片
//        try {
        // 将图片转换成字符串
            /*File imgFile = new File("f:\\Vista.png");
            FileInputStream fis = new FileInputStream( imgFile );
            byte[] bytes = new byte[fis.available()];
            fis.read(bytes);
            fis.close();

            String imgStr = byte2hex( bytes );*/
        //System.out.println( imgStr);


        // 将字符串转换成二进制，用于显示图片
            /*byte[] imgByte = imgFile.getBytes();
            InputStream in = new ByteArrayInputStream(imgByte);
            byte[] b = new byte[1024];
            int nRead = 0;
            OutputStream o = response.getOutputStream();
            while ((nRead = in.read(b)) != -1) {
                o.write(b, 0, nRead);
            }
            o.flush();
            o.close();
            in.close();

        } catch (Exception e) {
            e.printStackTrace();
        }*/
//        String temp = getImageBinary();
        int index = imgFile.indexOf(",");
        imgFile = imgFile.substring(index + 1);
        base64StringToImage(imgFile);
        return null;
    }

    private static void base64StringToImage(String base64String) {
        try {
            byte[] bytes1 = decoder.decodeBuffer(base64String);

            ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);
            BufferedImage bi1 = ImageIO.read(bais);
            File w2 = new File("I:\\tmp\\pis_test.jpg");//可以是jpg,png,gif格式
            ImageIO.write(bi1, "jpg", w2);//不管输出什么格式图片，此处不需改动
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private static String getImageBinary() {
        File f = new File("I:\\tmp\\pis\\55796421N636088ed.jpg");
        BufferedImage bi;
        try {
            bi = ImageIO.read(f);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(bi, "jpg", baos);
            byte[] bytes = baos.toByteArray();

            return encoder.encodeBuffer(bytes).trim();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    //文本编辑器
    @RequestMapping(value = "/umeditorIndex.html")
    public String umeditorIndex() throws Throwable {
        return "demo/umeditor";
    }

    @RequestMapping(value = "/ueditorIndex.html")
    public String ueditorIndex() throws Throwable {
        return "demo/ueditor";
    }

    @RequestMapping(value = "/ueditorController.html")
    public void ueditorController(HttpServletRequest request, HttpServletResponse response) throws Throwable {
        request.setCharacterEncoding("utf-8");
        response.setHeader("Content-Type", "text/html");
        String temp1 = request.getServletPath();
        String temp2 = request.getRealPath("/");
        String rootPath = request.getSession().getServletContext().getRealPath("/");
//        String temp3 = new ActionEnter(request, temp1).exec();
//        response.getWriter().write(new ActionEnter(request, temp1).exec());
//        response.getWriter().flush();
//        String rootPath = com.b
    }

    @RequestMapping(value = "/remodalController.html")
    public String remodalController() throws Throwable{
        return "demo/remodal";
    }

    public static void main(String[] args) {
        String imgFile = "";
        int index = imgFile.indexOf(",");
        imgFile = imgFile.substring(index);
        System.out.println(imgFile);
    }
}
